﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;

namespace Presentation
{
	public partial class CreateUser : System.Web.UI.Page
	{
		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			if( !Page.IsPostBack )
			{
				RadioTeacher.Checked = true;
			}

		}

		protected void submit_Click( object sender, EventArgs e )
		{
			bool isAdmin = false;
			bool isTeacher = false;
			if( RadioAdmin.Checked )
			{
				isAdmin = true;
			}
			else if( RadioTeacher.Checked )
			{
				isTeacher = true;
			}
			else if( RadioBoth.Checked )
			{
				isAdmin = true;
				isTeacher = true;
			}

			if( Vname.IsValid && Vneptun1.IsValid && Vneptun2.IsValid && Vemail.IsValid )
			{
				engine.CreateUser( Name.Text, engine.CreateHash( NEPTUN.Text ), Email.Text, NEPTUN.Text, isTeacher, isAdmin );
				Response.Redirect( "~/Admin/User.aspx?msg=1" );
			}
		}
	}
}